package parser;

import lexer.Token;
import java.util.ArrayList;
import java.util.List;

public class ASTNode {
    private NodeType type;
    private Token token;
    private List<ASTNode> children;
    private Object value;

    public ASTNode(NodeType type) {
        this.type = type;
        this.children = new ArrayList<>();
    }

    public ASTNode(NodeType type, Token token) {
        this(type);
        this.token = token;
    }

    public ASTNode(NodeType type, Object value) {
        this(type);
        this.value = value;
    }

    // Getters and setters
    public NodeType getType() { return type; }
    public Token getToken() { return token; }
    public List<ASTNode> getChildren() { return children; }
    public Object getValue() { return value; }

    public void addChild(ASTNode child) {
        children.add(child);
    }

    @Override
    public String toString() {
        return toString(0);
    }

    private String toString(int indent) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < indent; i++) {
            sb.append("  ");
        }

        sb.append(type);
        if (token != null) {
            sb.append(" [").append(token.getValue()).append("]");
        }
        if (value != null) {
            sb.append(" value=").append(value);
        }
        sb.append("\n");

        for (ASTNode child : children) {
            sb.append(child.toString(indent + 1));
        }

        return sb.toString();
    }
}